﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using GraphCompiler.Core;
using GraphCompiler.Core.Exceptions;
using GraphCompiler.Core.SyntaxRepresentation;
using GraphCompiler.Core.Parsing;
using Rhino.Mocks;
using StructureMap;

namespace GraphCompiler.Specification
{
    [TestFixture]
    public class ParserSpecs
    {
        Parser _parser;

        [SetUp]
        public void SetUp()
        {
            _parser = ObjectFactory.GetInstance<Parser>();
        }

        [Test]
        public void ReturnsEmptySyntaxNodeForEmptyTokenList()
        {
            var tokens = new TokenCollection();

            var expectedSyntaxNode = new SyntaxNode();

            var actualSyntaxNode = _parser.ParseTypedNode(tokens);

            Assert.AreEqual(expectedSyntaxNode, actualSyntaxNode);
        }

        [Test]
        public void ReturnsSingleDiagramNodeForEmptyDiagramDeclaration()
        {
            var tokens = new TokenCollection()
            { 
                new Token("diagram"),
                new Token("{"),
                new Token("}")
            };

            var expectedNode = new DiagramNode();

            var actualNode = _parser.ParseTypedNode(tokens);

            Assert.AreEqual(expectedNode, actualNode);
        }

        [Test]
        [ExpectedException(typeof(SyntaxErrorException))]
        public void RaisesErrorForInvalidTokenSequence()
        {
            var tokens = new TokenCollection()
            { 
                new Token("invalididentifier"),
                new Token("!"),
                new Token("{")
            };

            _parser.ParseTypedNode(tokens);
        }

        [Test]
        [ExpectedException(typeof(SyntaxErrorException), ExpectedMessage="{", MatchType=MessageMatch.Contains)]
        public void GivesErrorMessageContainingExpectedTokenForInvalidTokenSequence()
        {
            var tokens = new TokenCollection()
            { 
                new Token("diagram"),
                new Token("!"),
                new Token("}")
            };

            _parser.ParseTypedNode(tokens);
        }

        [Test]
        [ExpectedException(typeof(SyntaxErrorException), ExpectedMessage = "!", MatchType = MessageMatch.Contains)]
        public void GivesErrorMessageContainingActualTokenForInvalidTokenSequence()
        {
            var tokens = new TokenCollection()
            { 
                new Token("diagram"),
                new Token("!"),
                new Token("}")
            };

            _parser.ParseTypedNode(tokens);
        }

        [Test]
        public void GivenDiagramWithTwoBoxesAndAnArrow_ReturnsCorrespondingSyntaxNodeHierarchy()
        {

        }
    }
}
